<!--
 * @Author: 王灿
 * @Date: 2019-11-28 08:37:58
 * @LastEditTime: 2019-12-10 10:34:37
 * @Description: 地址修改
 * @FilePath: \scarlet-sparrow-admin\src\views\order\order-address\address-edit\index.vue
 -->
<template>
  <div>
    <el-form :model="ruleForm" ref="ruleForm" :rules="rule">
      <!-- 地址 -->
      <el-form-item prop="receiveAddress">
        <el-row>
          <el-input v-model="ruleForm.receiveAddress" placeholder="收货地址" clearable></el-input>
        </el-row>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="onSubmitForm('ruleForm')">确 定</el-button>
      <el-button @click="onCancelForm('ruleForm')">取 消</el-button>
    </div>
  </div>
</template>

<script>
import { getAddressUpdate } from '@/api/order.js'
export default {
  name: 'AddressEdit',
  props: {
    // 默认数据
    orderId: {
      type: Number,
      default: 0
    },
    receiveAddress: {
         type: String,
      default: ''
    }
  },
  data() {
    return {
      ruleForm: {
        receiveAddress: ''
      },
      rule: {
        receiveAddress: [
          { required: true, message: '请输入收货地址', trigger: 'blur' }
        ]
      }
    }
  },
  watch: {
      // 数据变化
      receiveAddress() {
          this.ruleForm.receiveAddress = this.receiveAddress
      }
  },
  created() {
      this.ruleForm.receiveAddress = this.receiveAddress
  },
  methods: {
    // 提交
    onSubmitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          let form = new FormData()
          form.append('orderId', this.orderId)
          form.append('receiveAddress', this.ruleForm.receiveAddress)
          getAddressUpdate(form).then(res => {
            this.$notify.success({
              title: '提示',
              message: '编辑成功'
            })
            this.onRefresh()
          })
        }
      })
    },
    // 取消
    onCancelForm(formName) {
      this.$refs[formName].resetFields()
      let data = { isRefresh: false }
      this.$emit('onCancelBtn', data)
    },
    // 跳转课程页
    onRefresh() {
      setTimeout(() => {
        let data = { isRefresh: true }
        this.$emit('onCancelBtn', data)
      }, 1000)
    }
  }
}
</script>